package com.test.mooan.labama.view_boom.module_boom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.test.mooan.labama.R;
import com.test.mooan.labama.utils_boom.Threadutils_boom;
import com.test.mooan.labama.bean_boom.user_info_boom.ActList_boom;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class ShowWltActListView_Fr_boom extends RelativeLayout {

    private LinearLayout accountListLayout;

    public ShowWltActListView_Fr_boom(Context context) {
        super(context);
        initView(context);
    }

    public ShowWltActListView_Fr_boom(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ShowWltActListView_Fr_boom(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public ShowWltActListView_Fr_boom(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    public void initView(Context context) {
        View view = View.inflate(context, R.layout.view_show_wlt_act_list_fr_boom, this);
        accountListLayout = view.findViewById(R.id.view_show_wallet_account_list_layout_account_list);
    }

    //更新账户列表
    public void updateAccountList(){
        accountListLayout.removeAllViews();
        ArrayList<ActList_boom.BankAccount> accounts = ActList_boom.getInstance().gBankAccountList();
        for (ActList_boom.BankAccount account:accounts){
            addAccount(account);
        }
    }

    private void addAccount(ActList_boom.BankAccount bankAccount){
        View view = View.inflate(getContext(), R.layout.item_show_bk_act_fr_boom, null);
        TextView accountTv = view.findViewById(R.id.item_show_bank_account_tv_account);
        ImageView accountTypeIv = view.findViewById(R.id.item_show_bank_account_iv_wallet_type);
        TextView accountTypeTv = view.findViewById(R.id.item_show_bank_account_tv_wallet_type);

        accountTv.setText(bankAccount.gWalletMobile());
        accountTypeTv.setText(bankAccount.gWalletDesc());

        //获取图标
        if (bankAccount.gBitmap()!=null){
            accountTypeIv.setImageBitmap(bankAccount.gBitmap());
        }else {
            Threadutils_boom.getInstance().runOnChildThread(() -> {
                try {
                    URL url = new URL(bankAccount.gWalletLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        bankAccount.sBitmap(bitmap);
                        Threadutils_boom.getInstance().runOnUiThread(() -> accountTypeIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }

        accountListLayout.addView(view);
    }

}
